package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.YouxuanGoodsPreferential;
import com.ruoyi.system.mapper.YouxuanGoodsPreferentialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YouxuanPreferentialMapper;
import com.ruoyi.system.domain.YouxuanPreferential;
import com.ruoyi.system.service.IYouxuanPreferentialService;

/**
 * 优惠管理Service业务层处理
 *
 * @author admin
 * @date 2021-01-12
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class YouxuanPreferentialServiceImpl implements IYouxuanPreferentialService {
    @Autowired
    private YouxuanPreferentialMapper youxuanPreferentialMapper;

    @Autowired
    private YouxuanGoodsPreferentialMapper youxuanGoodsPreferentialMapper;
    /**
     * 查询优惠管理
     *
     * @param id 优惠管理ID
     * @return 优惠管理
     */
    @Override
    public YouxuanPreferential selectYouxuanPreferentialById(Long id) {
        YouxuanPreferential y = youxuanPreferentialMapper.selectYouxuanPreferentialById(id);
        if (y.getTargets() != null && !y.getTargets().equals(""))
            y.setTargetsArray(y.getTargets().split(","));
        return y;
    }

    /**
     * 查询优惠管理列表
     *
     * @param youxuanPreferential 优惠管理
     * @return 优惠管理
     */
    @Override
    public List<YouxuanPreferential> selectYouxuanPreferentialList(YouxuanPreferential youxuanPreferential) {
        List<YouxuanPreferential> list = youxuanPreferentialMapper.selectYouxuanPreferentialList(youxuanPreferential);

        for (YouxuanPreferential y : list
        ) {
            if (y.getTargets() != null && !y.getTargets().equals(""))
                y.setTargetsArray(y.getTargets().split(","));
        }
        return list;
    }

    /**
     * 新增优惠管理
     *
     * @param youxuanPreferential 优惠管理
     * @return 结果
     */
    @Override
    public int insertYouxuanPreferential(YouxuanPreferential youxuanPreferential) {
        StringBuffer sb = new StringBuffer();
        String[] targetsArray = youxuanPreferential.getTargetsArray();
        for (int i = 0; i < targetsArray.length; i++) {
            if (i != youxuanPreferential.getTargetsArray().length - 1)
                sb.append(youxuanPreferential.getTargetsArray()[i] + ",");
            else
                sb.append(youxuanPreferential.getTargetsArray()[i]);
        }
        youxuanPreferential.setTargets(sb.toString());
        int ret = youxuanPreferentialMapper.insertYouxuanPreferential(youxuanPreferential);

        insertYouxuanGoodsPreferentialBatch(youxuanPreferential.getId(),targetsArray);

        return ret;
    }

    public int insertYouxuanGoodsPreferentialBatch(Long id,String[] targetsArray){
        List<YouxuanGoodsPreferential> youxuanGoodsPreferentials = new ArrayList<YouxuanGoodsPreferential>();
        for (int i = 0; i < targetsArray.length; i++) {
            YouxuanGoodsPreferential youxuanGoodsPreferential = new YouxuanGoodsPreferential();
            youxuanGoodsPreferential.setPreferentialId(id);
            youxuanGoodsPreferential.setGoodsId(Long.valueOf(targetsArray[i]));
            youxuanGoodsPreferential.setUpdateTime(new Date());
            youxuanGoodsPreferential.setCreateTime(new Date());
            youxuanGoodsPreferentials.add(youxuanGoodsPreferential);
        }
        int i = youxuanGoodsPreferentialMapper.insertYouxuanGoodsPreferentialBatch(youxuanGoodsPreferentials);
        return i;
    }

    /**
     * 修改优惠管理
     *
     * @param youxuanPreferential 优惠管理
     * @return 结果
     */
    @Override
    public int updateYouxuanPreferential(YouxuanPreferential youxuanPreferential) {
        StringBuffer sb = new StringBuffer();

        String[] targetsArray = youxuanPreferential.getTargetsArray();
        for (int i = 0; i < targetsArray.length; i++) {
            if (i != youxuanPreferential.getTargetsArray().length - 1)
                sb.append(youxuanPreferential.getTargetsArray()[i] + ",");
            else
                sb.append(youxuanPreferential.getTargetsArray()[i]);
        }

        int retId = youxuanPreferentialMapper.updateYouxuanPreferential(youxuanPreferential);
        Long id = youxuanPreferential.getId();
        youxuanGoodsPreferentialMapper.deleteYouxuanGoodsPreferentialAll(id);

        insertYouxuanGoodsPreferentialBatch(id,targetsArray);
        return retId;
    }

    /**
     * 批量删除优惠管理
     *
     * @param ids 需要删除的优惠管理ID
     * @return 结果
     */
    @Override
    public int deleteYouxuanPreferentialByIds(Long[] ids) {
        youxuanGoodsPreferentialMapper.deleteYouxuanGoodsPreferentialByPIdBatch(ids);
        return youxuanPreferentialMapper.deleteYouxuanPreferentialByIds(ids);
    }

    /**
     * 删除优惠管理信息
     *
     * @param id 优惠管理ID
     * @return 结果
     */
    @Override
    public int deleteYouxuanPreferentialById(Long id) {
        return youxuanPreferentialMapper.deleteYouxuanPreferentialById(id);
    }
}
